package com.vpen.blog.controller;

import com.vpen.blog.pojo.Book;
import com.vpen.blog.repository.BookRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import org.springframework.data.domain.Pageable;
import java.util.List;

/**
 * -@Version 1.0
 * -@Author:Vpen
 * -@Date:2021/7/5 9:17
 * -@Content:
 */
@RestController
@RequestMapping("/api/book")
public class BookController {
    @Resource
    private BookRepository bookRepository;

    // 每页个数
    private static final Integer SIZE=15;

    @GetMapping("/findAllBook")
    public List<Book> findAllBook(){
        return bookRepository.findAll();
    }

//    分页
    @GetMapping("/pageBook/{page}")
    public Page<Book> pageBook(@PathVariable("page") Integer page){
        Pageable pageable = PageRequest.of(page-1,SIZE);
        return bookRepository.findAll(pageable);
    }

//    添加图书
    @PostMapping("/addBook")
    public String addBook(@RequestBody Book book){
        Book book1 = bookRepository.save(book);
        System.out.println(book1);
        return "success";
    }
//    修改图书
    @PutMapping("/updateBook")
    public String updateBook(@RequestBody Book book){
        Book book1 = bookRepository.save(book);
        return "success";
    }

    // 删除图书
    @PostMapping("/deleteBook")
    public void deleteBook(@RequestBody Book book){
        bookRepository.delete(book);
    }

}
